package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.CoordinateEntity;
import com.hualude.roadsign.repository.CoordinateRepository;
import com.hualude.roadsign.repository.RoadLineFaultRepository;
import com.hualude.roadsign.repository.RoadLineRepository;
import com.hualude.roadsign.service.RoadLineManageService;
import com.hualude.roadsign.utils.FormatNumber;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/8/17.
 */
@Service
public class RoadLineManageServiceImpl implements RoadLineManageService {
    @Resource
    private RoadLineRepository roadLineRepository;
    @Resource
    private CoordinateRepository coordinateRepository;
    @Resource
    private RoadLineFaultRepository roadLineFaultRepository;

    @Override
    public DTResponseDto getRoadLine4DT(DTRequestDto dtRequestDto,
                                        String actorIds,
                                        Integer orgId,
                                        Boolean audit,
                                        String startRoad,
                                        String endRoad,
                                        String roadName) throws Exception {
        List<RoadLineDto> roadLineDtos = roadLineRepository.getRoadLine4DT(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName);
        Integer count = roadLineRepository.getRoadLine4DTCount(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, roadLineDtos);
    }

    @Override
    @Transactional
    public Integer deleteRoadLine(String roadLineIds) throws Exception {
        String[] roadLineIdArray = roadLineIds.split(",");
        for (String roadLineId : roadLineIdArray) {
            roadLineFaultRepository.deleteLineFault(Integer.parseInt(roadLineId));
            coordinateRepository.deleteCoordinateByTypeId(Integer.parseInt(roadLineId), 1);
            roadLineRepository.deleteRoadLine(Integer.parseInt(roadLineId));
        }
        return roadLineIdArray.length;
    }

    @Override
    @Transactional
    public Integer auditRoadLine(String roadLineIds) throws Exception {
        String[] roadLineIdArray = roadLineIds.split(",");
        for (String roadLineId : roadLineIdArray) {
            roadLineRepository.auditRoadLine(Integer.parseInt(roadLineId));
        }
        return roadLineIdArray.length;
    }

    @Override
    public List<String> getStartRoadByRoadName(String roadName, String actorIds, Integer orgId) throws Exception {
        return roadLineRepository.getStartRoadByRoadName(roadName, actorIds, orgId);
    }

    @Override
    public List<String> getEndRoadByStartRoad(String roadLineRoadName, String startRoad, String actorIds, Integer orgId) throws Exception {
        return roadLineRepository.getEndRoadByStartRoad(roadLineRoadName, startRoad, actorIds, orgId);
    }

    @Override
    @Transactional
    public Integer addRoadLine(RoadLineDto roadLineDto) throws Exception {

        Integer roadLineId = roadLineRepository.save(roadLineDto.toBean()).getRoadLineId();
        String points = roadLineDto.getRoadLinePoints();
        String[] pointArray = points.split(";");
        for (int i = 0; i < pointArray.length; i++) {
            String[] point = pointArray[i].split(",");
            CoordinateEntity coordinateEntity = new CoordinateEntity();
            coordinateEntity.setCoordinateTypeId(roadLineId);
            coordinateEntity.setCoordinateLongitude(FormatNumber.resize(point[0]));
            coordinateEntity.setCoordinateLatitude(FormatNumber.resize(point[1]));
            coordinateEntity.setCoordinateOrder(i);
            coordinateEntity.setCoordinateType(1);
            coordinateRepository.save(coordinateEntity).getCoordinateId();
        }
        return roadLineId;
    }

    @Override
    @Transactional
    public Integer updateRoadLine(RoadLineDto roadLineDto) throws Exception {
        if (roadLineDto.getRoadLinePoints().equals("no") == false) {
            coordinateRepository.deleteCoordinateByTypeId(roadLineDto.getRoadLineId(), 1);
            String points = roadLineDto.getRoadLinePoints();
            String[] pointArray = points.split(";");
            for (int i = 0; i < pointArray.length; i++) {
                String[] point = pointArray[i].split(",");
                CoordinateEntity coordinateEntity = new CoordinateEntity();
                coordinateEntity.setCoordinateTypeId(roadLineDto.getRoadLineId());
                coordinateEntity.setCoordinateLongitude(point[0]);
                coordinateEntity.setCoordinateLatitude(point[1]);
                coordinateEntity.setCoordinateOrder(i);
                coordinateEntity.setCoordinateType(1);
                coordinateRepository.save(coordinateEntity).getCoordinateId();
            }
        }
        return roadLineRepository.updateRoadLine(
                roadLineDto.getRoadLineId(),
                roadLineDto.getProjectNumber(),
                roadLineDto.getRoadLineProvince(),
                roadLineDto.getRoadLineCity(),
                roadLineDto.getRoadLineArea(),
                roadLineDto.getRoadLineRoadName(),
                roadLineDto.getRoadLineStartRoad(),
                roadLineDto.getRoadLineEndRoad(),
                roadLineDto.getRoadLineType(),
                roadLineDto.getRoadLineGraphic(),
                roadLineDto.getRoadLinePhoto(),
                roadLineDto.getRoadLineInstallTime(),
                roadLineDto.getRoadLineInstallOrg(),
                roadLineDto.getRoadLinePeriod(),
                roadLineDto.getRoadLineServiceLife(),
                roadLineDto.getRoadLineRemark(),
                roadLineDto.getRoadLineCreatorId(),
                roadLineDto.getRoadLineModifierId(),
                roadLineDto.getRoadLineOrgId(),
                roadLineDto.getRoadLineMainOrgId(),
                roadLineDto.getRoadLineMileage()
                );
    }

    @Override
    public List<CoordinateDto> getCoordinatesById(Integer roadLineId, Integer type) throws Exception {
        return coordinateRepository.getCoordinatesById(roadLineId, type);
    }

    @Override
    public List<String> getCity4RoadLine(String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return roadLineRepository.getCity4RoadLine(actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getStartRoadByCity(String city, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return roadLineRepository.getStartRoadByCity(city, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getSectionByStartRoad4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return roadLineRepository.getSectionByStartRoad4App(city, roadName, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<RoadLineDto> getRoadLine4APP(String actorIds, Integer orgId, Integer mCount, Integer mSize, String city, String startRoad, String endRoad, String roadName) throws Exception {
        return roadLineRepository.getRoadLine4APP(actorIds, orgId, mCount, mSize, city, startRoad, endRoad, roadName);
    }

    @Override
    public RoadLineDto getRoadLineById(Integer roadLineId) throws Exception {
        List<CoordinateDto> coordinateDtos = coordinateRepository.getCoordinatesById(roadLineId, 1);
        RoadLineDto roadLineDto = roadLineRepository.getRoadLineById(roadLineId);
        roadLineDto.setLatitude(coordinateDtos.get(0).getCoordinateLatitude());
        roadLineDto.setLongitude(coordinateDtos.get(0).getCoordinateLongitude());
        return roadLineDto;
    }

    @Override
    public List<String> getRoadName4RoadLineByCity(String actorIds, Integer orgId, String city, Integer mCount, Integer mSize) throws Exception {
        return roadLineRepository.getRoadName4RoadLineByCity(actorIds, orgId, city, mCount, mSize);
    }

    @Override
    public List<String> getStartRoadByRoadName4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return roadLineRepository.getStartRoadByRoadName4App(city, roadName, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getEndRoadByStartRoad4App(String city, String roadName, String startRoad, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return roadLineRepository.getEndRoadByStartRoad4App(city,roadName , startRoad, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<RoadLineDto> getRoadLine4Map4App(Integer orgId,
                                                 String actorIds,
                                                 double neLongitude,
                                                 double neLatitude,
                                                 double swLongitude,
                                                 double swLatitude) throws Exception {
        List<RoadLineDto> roadLineDtos = roadLineRepository.getRoadLine4Map4App
                (orgId, actorIds, neLongitude, neLatitude, swLongitude, swLatitude);
        for(RoadLineDto roadLineDto : roadLineDtos) {
            List<CoordinateDto> coordinateDtos = coordinateRepository.getCoordinatesById(roadLineDto.getRoadLineId(), 1);
            roadLineDto.setCoordinateDtos(coordinateDtos);
        }
        return roadLineDtos;
    }
}
